
import os

def get_distance(x1, y1, x2, y2):
    """ 
    input: the coordinates of two points 
    returns: the distance between the two points 
             if they are aligned either vertically or horizontally, 
             -1 otherwise
    """
    if x1 != x2 and y1 != y2:
        return -1
    return max(abs(x1 - x2), abs(y1 - y2)) 
    
def string_to_int(list):
    """ 
    takes in a list of strings and translates 
    each item into int and returns the list of translated items 
    """
    list2 = []
    for item in list:
        list2.append(int(item))
    return list2

def read_input():
    """ 
    opens the text file and extract the dimensions of 
    the each block at put them into list of tuples 
    """
    ret = []
    f = open(os.path.join("data","input33.txt"), "r")    
    for line in f:
        list = line.split(";")
        list = list[0].split();    
        if list[0] == "DIMENSIONS" and len(list) == 9:
            list = string_to_int(list[1:])        
            side1 = get_distance(list[0], list[1], list[2], list[3])
            side2 = get_distance(list[0], list[1], list[6], list[7])
            #print "The sides are", side1, side2
            item = dict(w=side1, h=side2);
            ret.append(item)
    f.close()
    return ret[:-1]
